package com.qzsoft.system.modules.permission.index.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.jeemis.common.annotation.LogOperation;
import com.qzsoft.jeemis.common.constant.Constant;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.DefaultGroup;
import com.qzsoft.system.modules.permission.deptJob.dto.SysDeptJobDTO;
import com.qzsoft.system.modules.permission.index.dto.FlowDTO;
import com.qzsoft.system.modules.permission.index.service.IndexService;
import com.qzsoft.system.repository.sys.entity.SysDeptJobEntity;
import com.qzsoft.system.repository.sys.entity.SysScheduleEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
@Api(tags = "")
@Log4j2
public class IndexController {
    @Autowired
    private IndexService indexService;

    @GetMapping("flow/page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题", paramType = "query", dataType = "String")
    })
    public Result<IPage<FlowDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        IPage<FlowDTO> page = indexService.selectFlowPage(params);
        return new Result<IPage<FlowDTO>>().ok(page);
    }

    @GetMapping("flow/list")
    @ApiOperation("列表")
    public Result<List<Map<String, String>>> flowList(@ApiIgnore @RequestParam Map<String, Object> params) throws Exception {
        List<Map<String, String>> list = indexService.selectFlowList(params);
        return new Result<List<Map<String, String>>>().ok(list);
    }

    @PostMapping("schedule/save")
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result saveSchedule(@RequestBody SysScheduleEntity entity){
        //效验数据
        ValidatorUtils.validateEntity(entity, AddGroup.class, DefaultGroup.class);
        indexService.saveSchedule(entity);
        return new Result();
    }

    @GetMapping("schedule/list")
    @ApiOperation("列表")
    public Result<List<Map<String, Object>>> scheduleList(@ApiIgnore @RequestParam Map<String, Object> params) throws Exception {
        List<Map<String, Object>> list = indexService.selectScheduleList(params);
        return new Result<List<Map<String, Object>>>().ok(list);
    }
}
